﻿using BW.Mes.WinForms.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace BW.Mes.WinForms
{
    public static class HttpClientHepler
    {
         public async static Task<string>  RequsetUrl(RequestType method, string url,string data="")
        {
            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri(Settings.Default.WareHouseUrl);
                client.DefaultRequestHeaders.Accept.Clear();
                HttpResponseMessage response = null;


                switch (method)
                {
                    case RequestType.get:
                      response = await client.GetAsync(url);
                        break;
                    case RequestType.post:
                        HttpContent httpContent =new StringContent(data, Encoding.UTF8, "application/json");
                        response = await client.PostAsync(url, httpContent);
                        break;
                    case RequestType.put:
                        httpContent = new StringContent(data, Encoding.UTF8, "application/json");
                        response = await client.PutAsync(url, httpContent);
                        break;
                    case RequestType.delete:
                        response = await client.DeleteAsync(url);
                        break;
                }
                if (response.IsSuccessStatusCode)
                {
                    return await response.Content.ReadAsStringAsync();
                }
                return string .Empty;
            }
        }
    }
}
